from urlparse import urlparse

from nevow import tags as t

import const
from axiomstore import WikiWord

def parseWikiWords(content=None, store=None):
    newContent = []
    for word in content.split(' '):
        if const.matchWikiWord(word):
            data = {'url': word, 'text': word}
            check = unicode(word)
            result = store.findFirst(WikiWord, WikiWord.word == check)
            # check to see if it exists and set the style
            if result and result.ispage:
                data['marker'] = ''
                data['style'] = ''
            else:
                data['marker'] = '?'
                data['style'] = 'class="wikiWord"'
            # build the link
            template = '<a href="%(url)s" %(style)s>%(text)s%(marker)s</a>'
            word = template % data
        newContent.append(word)
    return ' '.join(newContent)

def makeWikiWord(text):
    return ''.join([x.capitalize() for x in text.split(' ')]).replace(' ', '')

def getQueryString(url):
    return urlparse(url)[const.QUERY]


